<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Feedback;
use App\Models\Feedbacks;
use App\Models\rewardOrder;
use App\Models\Demands;
use App\Models\Users;
use Illuminate\Http\Request;
use App\Models\BalanceLogs;
use App\Models\OrderDetail;
use App\Models\Mails;
class FeedbackController extends Controller {

    /**
     * 页面
     * @return view
     */
    public function index(){
        return view('admin.setting.feedback');
    }

    /**
     * 列表数据
     * @param Request $request
     * @return bool
     */
    public function list(Request $request){
        try{
            $params = [
                'kwd' => (string)$request->get('kwd',''),                
                'status' => (int)$request->get('status',0)
            ];
            $lists = Feedbacks::getList($params);
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 编辑
     * @param Request $request
     * @return bool|view
     */
    public function edit(Request $request){
        try{
            $id = (int)$request->get('id',0);
            $info = [];
            if($id){
                $info = Feedbacks::findOrFail($id);
                $info['reward'] = Demands::findOrFail($info['reward_id']);
                $rewardOrder = rewardOrder::findOrFail($info['order_id']);
                $info['reward_order'] = $rewardOrder;
                $detail = OrderDetail::where(array('order_id'=>$rewardOrder['id'],'user_id'=>$rewardOrder['user_id']))->first();
                $info['order_detail'] =$detail;
            }
           
            $info = json_encode($info);

            return view('admin.setting.feedback_update',compact('info','id'));
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 更新
     * @param Feedback $request
     * @return bool
     */
    public function update(Request $request)
    {
        try{
            $data = $request->all();
            if($data['id']){
            	$info = Feedbacks::findOrFail($data['id']);
            	$info->update($data);
            
                if($data['status'] == 2){
                	
                	//任务通过之后将任务状态改为完成并赋予收益
                	$order['status'] = 3;
                	$order['check_status'] = 1;
                	$rewardOrder = rewardOrder::findOrFail($info['order_id']);
                	$rewardOrder->update($order);
                	
                	//新增余额
                	Users::where('id',$info['user_id'])->increment('balance',$rewardOrder['get_money']);
                	$reward = Demands::findOrFail($rewardOrder['reward_id']);
                	
                	//悬赏这扣除余额
                	Users::where('id',$reward['user_id'])->decrement('balance',$reward['price']);
                	$balance = [
                			'user_id' => $reward['user_id'],
                			'user_name' => Users::where('id',$reward['user_id'])->value('nickname'),
                			'money' => $reward['price'],
                			'balance' => '-'.$reward['price'],
                				
                			'desc' => '支付赏金',
                			'type' => 1,
                	];
                	$balance_info = BalanceLogs::create($balance);
                	
                	//成功数+1
                	Demands::where('id',$rewardOrder['reward_id'])->increment('ownnumber');
                	
                	
                	
                	$content = '您接受的任务"' .$reward['title'] .'"申述已处理，任务通过，赏金增加'.$rewardOrder['get_money'];
                	
                	$template = [
                			'title' => '任务通告',
                			'content' => $content,
                			'user_id' => $rewardOrder['user_id'],
                			'status' => 1,
                			'type' => 1
                	];
                	Mails::create($template);
                	
                	$user_info = Users::where('id',$rewardOrder['user_id'])->where('status','<>',-1)->first(['true_name','nickname','inviter_id']);
                	$balance = [
                			'user_id' => $info['user_id'],
                			'user_name' => $user_info['true_name']?$user_info['true_name']:$user_info['nickname'],
                			'money' => $rewardOrder['get_money'],
                			'balance' => '+'.$rewardOrder['get_money'],
                			 
                			'desc' => '任务收益',
                			'type' => 1,
                	];
                	$balance_info = BalanceLogs::create($balance);
                	
                	if($user_info['inviter_id']>0){
                		//有上级，查看是否有返佣资格
                		$my_numbers = Users::where(array("inviter_id"=>$user_info['inviter_id'],"type"=>2,"status"=>2))->count("id");
                		if($my_numbers>=10){
                			$base = Configs::getConfig('base');
                			$demandprice = Demands::where('id',$rewardOrder['reward_id'])->value('price');
                	
                			Users::where('id',$user_info['inviter_id'])->increment('balance',$rewardOrder['get_money']*$base['captain_rate']/100);
                			$msg = '您的团员'.$user_info['nickname'].'完成任务"' .$reward['title'] .'"获得佣金'.$rewardOrder['get_money']*$base['captain_rate']/100;
                			$template1 = [
                					'title' => '佣金提成通告',
                					'content' => $msg,
                					'user_id' => $user_info['inviter_id'],
                					'status' => 1,
                					'type' => 1
                			];
                			//站内信
                			Mails::create($template1);
                	
                			$inviter = Users::where('id',$user_info['inviter_id'])->where('status','<>',-1)->first(['true_name','nickname','inviter_id']);
                			$balance = [
                					'user_id' => $user_info['inviter_id'],
                					'user_name' => $inviter['true_name']?$inviter['true_name']:$inviter['nickname'],
                					'money' => $rewardOrder['get_money']*$base['captain_rate']/100,
                					'balance' => '+'.$rewardOrder['get_money']*$base['captain_rate']/100,
                	
                					'desc' => '团员收益提成',
                					'type' => 1,
                			];
                			$balance_info = BalanceLogs::create($balance);
                	
                		}
                	}
                }else{
                	$order['status'] = 4;
                	$order['check_status'] = 2;
                	$rewardOrder = rewardOrder::findOrFail($info['order_id']);
                	$rewardOrder->update($order);
                	$reward = Demands::findOrFail($rewardOrder['reward_id']);
                	$content = '您接受的任务"' .$reward['title'] .'"申述不通过，任务失败。原因是：'.$data['remark'];
                	$template = [
                			'title' => '任务通告',
                			'content' => $content,
                			'user_id' => $rewardOrder['user_id'],
                			'status' => 1,
                			'type' => 1
                	];
                	Mails::create($template);
                }
                
                return $this->writeJson(0,null,'修改信息成功');
            }else{
            	return $this->writeJson(1,null,'数据异常');
            }
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 拒绝
     * @param Request $request
     * @return bool
     */
    public function lock(Request $request){
        try{
            $id = (int)$request->get('id',0);
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            $info = Feedbacks::findOrFail($id);
            $info->update(['status' => Feedbacks::STATUS_3]);
            
           
            $order['status'] = 4;
            $order['check_status'] = 2;
            $rewardOrder = rewardOrder::findOrFail($info['order_id']);
            $rewardOrder->update($order);
            $content = '您接受的任务"' .$reward['title'] .'"申述不通过，任务失败。请到任务中心查看详情';
            $template = [
            		'title' => '任务通告',
            		'content' => $content,
            		'user_id' => $rewardOrder['user_id'],
            		'status' => 1,
            		'type' => 1
            ];
            Mails::create($template);
            
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 通过
     * @param Request $request
     * @return bool
     */
    public function active(Request $request){
        try{
            $id = (int)$request->get('id',0);
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            $info = Feedbacks::findOrFail($id);
            $info->update(['status' => Feedbacks::STATUS_2]);
            
            //任务通过之后将任务状态改为完成并赋予收益
            $order['status'] = 3;
            $order['check_status'] = 1;
            $rewardOrder = rewardOrder::findOrFail($info['order_id']);
            $rewardOrder->update($order);

            //新增余额
            Users::where('id',$info['user_id'])->increment('balance',$rewardOrder['get_money']);
            //成功数+1
            Demands::where('id',$rewardOrder['reward_id'])->increment('ownnumber');
            $reward = Demands::findOrFail($rewardOrder['reward_id']);
            $content = '您接受的任务"' .$reward['title'] .'"申述已处理，任务通过，赏金增加'.$rewardOrder['get_money'];
            $template = [
            		'title' => '任务通告',
            		'content' => $content,
            		'user_id' => $rewardOrder['user_id'],
            		'status' => 1,
            		'type' => 1
            ];
            //站内信
            Mails::create($template);
            
            $user_info = Users::where('id',$rewardOrder['user_id'])->where('status','<>',-1)->first(['true_name','nickname','inviter_id']);
            $balance = [
            		'user_id' => $info['user_id'],
            		'user_name' => $user_info['true_name']?$user_info['true_name']:$user_info['nickname'],
            		'money' => $rewardOrder['get_money'],
            		'balance' => '+'.$rewardOrder['get_money'],
            			
            		'desc' => '任务收益',
            		'type' => 1,
            ];
            $balance_info = BalanceLogs::create($balance);
            
            if($user_info['inviter_id']>0){
            	//有上级，查看是否有返佣资格
            	$my_numbers = Users::where(array("inviter_id"=>$user_info['inviter_id'],"type"=>2,"status"=>2))->count("id");
            	if($my_numbers>=10){
            		$base = Configs::getConfig('base');
            		$demandprice = Demands::where('id',$rewardOrder['reward_id'])->value('price');
            		
            		Users::where('id',$user_info['inviter_id'])->increment('balance',$rewardOrder['get_money']*$base['captain_rate']/100);
            		$msg = '您的团员'.$user_info['nickname'].'完成任务"' .$reward['title'] .'"获得佣金'.$rewardOrder['get_money']*$base['captain_rate']/100;
            		$template1 = [
            				'title' => '佣金提成通告',
            				'content' => $msg,
            				'user_id' => $user_info['inviter_id'],
            				'status' => 1,
            				'type' => 1
            		];
            		//站内信
            		Mails::create($template1);
            		
            		$inviter = Users::where('id',$user_info['inviter_id'])->where('status','<>',-1)->first(['true_name','nickname','inviter_id']);
            		$balance = [
            				'user_id' => $user_info['inviter_id'],
            				'user_name' => $inviter['true_name']?$inviter['true_name']:$inviter['nickname'],
            				'money' => $rewardOrder['get_money']*$base['captain_rate']/100,
            				'balance' => '+'.$rewardOrder['get_money']*$base['captain_rate']/100,
            
            				'desc' => '团员收益提成',
            				'type' => 1,
            		];
            		$balance_info = BalanceLogs::create($balance);
            		
            	}
            }
            
            
            
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 单个/多个删除
     * @param Request $request
     * @return bool
     */
    public function destroy(Request $request){
        try{
            $id = $request->get('id');
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            if(is_array($id)){
                $where['id'] = ['in',$id];
            }else{
                $where['id'] = $id;
            }
            //修改软删除 19-7-31
            $data['status'] = -1;
            Feedbacks::where($where)->update($data);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

}
